package com.ruoyi.supplier.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.supplier.domain.vo.UndoLogVo;
import com.ruoyi.supplier.domain.bo.UndoLogBo;
import com.ruoyi.supplier.service.IUndoLogService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * AT transaction mode undo table控制器
 * 前端访问路由地址为:/supplier/log
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/log")
public class UndoLogController extends BaseController {

    private final IUndoLogService iUndoLogService;

    /**
     * 查询AT transaction mode undo table列表
     */
    @SaCheckPermission("supplier:log:list")
    @GetMapping("/list")
    public TableDataInfo<UndoLogVo> list(UndoLogBo bo, PageQuery pageQuery) {
        return iUndoLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AT transaction mode undo table列表
     */
    @SaCheckPermission("supplier:log:export")
    @Log(title = "AT transaction mode undo table", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UndoLogBo bo, HttpServletResponse response) {
        List<UndoLogVo> list = iUndoLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "AT transaction mode undo table", UndoLogVo.class, response);
    }

    /**
     * 获取AT transaction mode undo table详细信息
     *
     * @param branchId 主键
     */
    @SaCheckPermission("supplier:log:query")
    @GetMapping("/{branchId}")
    public R<UndoLogVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long branchId) {
        return R.ok(iUndoLogService.queryById(branchId));
    }

    /**
     * 新增AT transaction mode undo table
     */
    @SaCheckPermission("supplier:log:add")
    @Log(title = "AT transaction mode undo table", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UndoLogBo bo) {
        return toAjax(iUndoLogService.insertByBo(bo));
    }

    /**
     * 修改AT transaction mode undo table
     */
    @SaCheckPermission("supplier:log:edit")
    @Log(title = "AT transaction mode undo table", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UndoLogBo bo) {
        return toAjax(iUndoLogService.updateByBo(bo));
    }

    /**
     * 删除AT transaction mode undo table
     *
     * @param branchIds 主键串
     */
    @SaCheckPermission("supplier:log:remove")
    @Log(title = "AT transaction mode undo table", businessType = BusinessType.DELETE)
    @DeleteMapping("/{branchIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] branchIds) {
        return toAjax(iUndoLogService.deleteWithValidByIds(Arrays.asList(branchIds), true));
    }
}
